import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:fsharing/core/models/business_level.dart';
import 'package:fsharing/core/models/business_type.dart';
import 'package:fsharing/core/viewmodels/pages/mine/manage/business_level_model.dart';
import 'package:fsharing/core/viewmodels/view_model.dart';
import 'package:fsharing/ui/widgets/background_container.dart';
import 'package:fsharing/ui/widgets/round_back_button.dart';
import 'package:provider/provider.dart';

class BusinessLevelPage extends StatelessWidget {
  final BusinessType businessType;

  BusinessLevelPage({
    Key key,
    @required this.businessType,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => BusinessLevelModel(Provider.of(context, listen: false), businessType),
      child: BackgroundContainer(
        child: Scaffold(
          appBar: AppBar(
            leading: RoundBackButton(),
            title: Text('业务级别'),
          ),
          body: _ViewStateWidget(),
        ),
      ),
    );
  }
}

class _ViewStateWidget extends StatelessWidget {
  const _ViewStateWidget({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    BusinessLevelModel model = Provider.of(context);
    switch (model.viewState) {
      case ViewState.busy:
        return Center(
          child: CircularProgressIndicator(),
        );
      case ViewState.error:
        return Center(
          child: Text(model.errorMessage),
        );
      case ViewState.success:
        return _SuccessWidget(
          list: model.list,
        );
      default:
        return Container();
    }
  }
}

class _SuccessWidget extends StatelessWidget {
  final List<BusinessLevel> list;

  const _SuccessWidget({
    Key key,
    @required this.list,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
      slivers: <Widget>[
        _HeaderWidget(),
        _ListWidget(
          list: list,
        ),
      ],
    );
  }
}

class _HeaderWidget extends StatelessWidget {
  const _HeaderWidget({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SliverToBoxAdapter(
      child: Container(
        alignment: Alignment.centerLeft,
        padding: EdgeInsets.only(left: 12),
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage('assets/images/business/img_header.png'),
          ),
        ),
        height: 45,
        child: Text(
          '选择业务级别',
          style: TextStyle(fontSize: 17),
        ),
      ),
    );
  }
}

class _ListWidget extends StatelessWidget {
  const _ListWidget({
    Key key,
    @required this.list,
  }) : super(key: key);

  final List<BusinessLevel> list;

  @override
  Widget build(BuildContext context) {
    return SliverFixedExtentList(
      delegate: SliverChildBuilderDelegate(
        (context, index) {
          return _ListItemWidget(
            businessLevel: list[index],
          );
        },
        childCount: list.length,
      ),
      itemExtent: 68,
    );
  }
}

class _ListItemWidget extends StatelessWidget {
  final BusinessLevel businessLevel;

  const _ListItemWidget({
    Key key,
    @required this.businessLevel,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return InkWell(
      child: Container(
        color: Colors.white,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            Container(),
            Row(
              children: <Widget>[
                SizedBox(width: 12),
                CachedNetworkImage(
                  imageUrl: businessLevel.cardImage,
                  width: 45,
                  height: 45,
                  fit: BoxFit.fill,
                ),
                SizedBox(width: 12),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Text(
                        businessLevel.businessTypeName,
                        style: TextStyle(color: Color(0xFF252525), fontSize: 17),
                      ),
                      Text(
                        '(${businessLevel.description})',
                        style: TextStyle(color: Color(0xFF999999), fontSize: 10),
                      ),
                    ],
                  ),
                ),
                Column(
                  children: <Widget>[
                    Text.rich(
                      TextSpan(
                        text: '',
                        style: TextStyle(
                          color: Color(0xFF999999),
                        ),
                        children: [
                          TextSpan(
                            text: businessLevel.businessLevelName,
                          )
                        ],
                      ),
                    ),
                  ],
                ),
                SizedBox(width: 12),
              ],
            ),
            Divider(
              height: 1,
              indent: 12,
              endIndent: 12,
              color: Color(0xFFE6E6E6),
            ),
          ],
        ),
      ),
      onTap: () => _onItemClicked(context),
    );
  }

  void _onItemClicked(BuildContext context) {
    // 导航店铺购物
    Navigator.of(context).pushNamed('/business/condition', arguments: businessLevel);
  }
}
